<template>
  <el-col :span="12">
    <label>{{labelName}}：</label>
    <el-input-number class="input-block"
      :max="999999" 
      :debounce="0"
      placeholder="输入名额"
      v-model="myValue"/>
  </el-col>
</template>
<script>
  import emitter from 'utils/emitter.js'
  export default {
    mixins: [emitter],
    props: {
      parentData: Array,
      data: Object,
      index: Number,
      parentIndex: Number
    },
    computed: {
      labelName () {
        // 跑步状态下 表格只有一列
        return this.editorData.group[0]
      }
    },
    watch: {
      myValue (newVal, oldVal) {
        console.log(newVal)
        let curNum = 0
        let totalNum = 0
        this.parentData.forEach((item, index) => {
          // parentIndex指渠道的索引
          // this.index 名额的索引
          // 每个渠道只有一项 如果循环到当前渠道时，则直接+最新值即可
          if (index === this.parentIndex) {
            curNum += newVal - 0
          } else {
            let lotteryNumbers = item.channelsGroup[this.index].lotteryNumbers
            lotteryNumbers = lotteryNumbers || 0
            curNum += lotteryNumbers
          }
          !totalNum && (totalNum = item.channelsGroup[this.index].num - 0)
        })
        if (curNum > totalNum) {
          this.$nextTick(() => {
            this.myValue = 0
          })
          return this.$message({
            message: `${this.editorData.group}名额已超过了名额限制数量`,
            type: 'warning'
          })
        }
        // this.$nextTick(_ => {
        this.$set(this.editorData, 'lotteryNumbers', newVal)
        // this.editorData.lotteryNumbers = newVal
        this.$emit('update:data', this.editorData)
        this.dispatch('ElFormItem', 'el.form.change', [newVal])
        this.dispatch('ElFormItem', 'el.form.blur', [newVal])
        // this.$parent && this.$parent.$parent && this.$parent.$parent.$emit('el.form.blur', newVal)
        // })
      }
    },
    data () {
      let value = this.data.lotteryNumbers - 0
      return {
        myValue: value || 0,
        editorData: this.data
      }
    }
  }
</script>
